"""
BeautifulSoup4  的學習
"""
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup as bs_4
import os
import dark_web.log as log
html = '''
<!DOCTYPE html>
<html dir="ltr" lang="zh-cmn-hans">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1" />

<title>暗网中文论坛 - 首页</title>



<!-- Self_Code Start -->
<!-- Self_Code End -->

<link href="./assets/css/font-awesome.min.css?assets_version=33" rel="stylesheet">
<link href="./styles/se_square_left/theme/stylesheet.css?assets_version=33" rel="stylesheet">
<link href="./styles/se_square_left/theme/zh_cmn_hans/stylesheet.css?assets_version=33" rel="stylesheet">




<!--[if lte IE 9]>
	<link href="./styles/se_square_left/theme/tweaks.css?assets_version=33" rel="stylesheet">
<![endif]-->





</head>
<body id="phpbb" class="nojs notouch section-index ltr ">


<div id="wrap" class="wrap">
	<a id="top" class="top-anchor" accesskey="t"></a>
	<div id="page-header">
		<div class="headerbar" role="banner">
					<div class="inner">

<!-- Self_Code Start -->
				<div style="float:left;">
					<a href="http://deepbbs3x2owgof4.onion" title="首页"><span class="site_logo" style="background-image:url(/pay/image/deepbbs.gif)"></span></a>
				</div>
				<div style="float:left;margin-top: 10px;color: #FFFFFF;">
					<span style="font-size: 20px;">暗网中文论坛</span><br>
					<span style="font-size: 14px;">询价-讨论-调研</span>
				</div>
				<div style="float:right;margin-top:25px;">
						<a href='' class='logo_right'></a>
				</div> 
<!-- Self_Code End -->

			</div>
					</div>
				<div class="navbar" role="navigation">
	<div class="inner">
	<ul>
			</ul>
	</div>
</div>
	</div>


	<a id="start_here" class="anchor"></a>
	<div id="page-body" class="page-body" role="main">



<!-- Self_Code Start -->
	<form method="post" action="./ucp.php?mode=login&amp;sid=7a5f7b7c838a0315e20e364fd6c05024" class="headerspace">
	<h3><a href="./ucp.php?mode=login&amp;sid=7a5f7b7c838a0315e20e364fd6c05024">登录</a>&nbsp; &bull; &nbsp;<a href="./ucp.php?mode=register&amp;sid=7a5f7b7c838a0315e20e364fd6c05024">注册</a></h3>
		<fieldset class="quick-login">
			<label for="username"><span>用户名：</span> <input type="text" tabindex="1" name="username" id="username" size="10" class="inputbox" title="用户名" /></label>
			<label for="password"><span>密码：</span> <input type="password" tabindex="2" name="password" id="password" size="10" class="inputbox" title="密码" autocomplete="off" /></label>
			<!-- Self_Code Start -->
<!-- Self_Code End -->
			<input type="submit" tabindex="5" name="login" value="登录" class="button2" />
			<input type="hidden" name="redirect" value="./index.php?sid=7a5f7b7c838a0315e20e364fd6c05024" />
<input type="hidden" name="creation_time" value="1573026247" />
<input type="hidden" name="form_token" value="1edecf41249887088975d19f77ac6f65852d462d" />

		</fieldset>
	</form>
<!-- Self_Code End -->

	</div>
</body>
</html>

'''

# #创建 Beautiful Soup 对象
# soup = BeautifulSoup(html)
#
# #格式化输出 soup 对象的内容
# print (soup.prettify())
bs_data = bs_4(html, "lxml")
# autim = bs_data.select_one('input[name="autim"]').attrs["value"]
# sid = bs_data.select_one('input[name="sid"]').attrs["value"]
form_token = bs_data.select_one('input[name="form_token"]').attrs["value"]
creation_time = bs_data.select_one('input[name="creation_time"]').attrs[
    "value"
]
login = {
    # "autim": autim,
    "creation_time": creation_time,
    "form_token": form_token,
    "login": "登录",
    "password": "",
    "redirect": [
        item.attrs["value"]
        for item in bs_data.select('input[name="redirect"]')
    ],
    # "sid": sid,
    "username": "",
}
print(login)
dir = os.path.abspath('..')
log.debug(dir)
file_path = dir + "\\test\\respone_file\\register_url_response2.html"
html_file = open(file_path, "r", -1, "utf-8")
register_url_response2 = html_file.read()

bs_data = bs_4(register_url_response2, "lxml")
qa_confirm_id = bs_data.select_one("#qa_confirm_id").attrs["value"]
qa_answer = str(
    bs_data.select_one("#register > div.panel.captcha-panel > div > fieldset > dl > dt > label").get_text())

log.debug(qa_confirm_id)
log.debug(qa_answer.split("："))
log.debug(qa_answer.split("：")[1])